package com.example.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Scholarship;
import com.example.mapper.ScholarshipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/scholarship")

public class ScholarshipController {

    @Autowired
    ScholarshipMapper scholarshipMapper;

    //这部分主要是有关奖学金表增删改查这几个功能，查询这里直接使用分页查询。这部分主要是参考B站up主青戈。同学生表

    @GetMapping("/page")  //分页
    public Result selectAll(@RequestParam String scholarship_name, @RequestParam Integer start, @RequestParam Integer pageSize)
    {
        List<Scholarship> scholarshipList = scholarshipMapper.selectPage(scholarship_name,start,pageSize);
        Integer total = scholarshipMapper.selectTotal(scholarship_name);
        Map<String ,Object> map = new HashMap<>();
        map.put("list",scholarshipList);
        map.put("total",total);
        return Result.success(map);
    }


    @PostMapping("/save")
    public Result save (@RequestBody Scholarship scholarship){
        scholarshipMapper.insert(scholarship);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Scholarship scholarship){
        scholarshipMapper.update(scholarship);
        return Result.success();
    }


    @DeleteMapping("/delete")
    public Result delete(@RequestParam  Integer scholarship_no){
        scholarshipMapper.delete(scholarship_no);
        return Result.success();
    }













/*    @Autowired
    private ScholarshipServiceImpl scholarshipService;

    @GetMapping("/page")
    public Page<Scholarship> page(

            @RequestParam(defaultValue = "") String scholarship_name,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "15") Integer size
    ) {
        return scholarshipService.page(current, size, scholarship_name);
    }

    @PostMapping("/save")
    public boolean save(@RequestBody Scholarship scholarship){
        return scholarshipService.saveOrUpdateById(scholarship);
    }

    @PostMapping("/delete")
    public boolean delete(@RequestBody List<Integer> scholarship_nos) {
        return scholarshipService.deleteBatchIds(scholarship_nos);
    }*/

}